<template>
  <div class="wrap">
    <header>搜索</header>
    <main class="bannerList" ref="myScroll">
      <div class="shop-left">
        <div class="left" ref="left">
          <li
            v-for="(item,index) in searchList"
            :key="index"
            :class="{on:index==selectIndex}"
            @click="toRight(index)"
          >{{item.name}}</li>
        </div>
      </div>
      <div class="shop-right">
        <ul class="right" ref="ul">
          <li v-for="(item,index) in searchList" :key="index">
            <div class="item_name">
              <h3>{{item.name}}</h3>
              <span>查看更多</span>
            </div>
            <div class="right-dl">
              <dl v-for="(v,k) in item.items" :key="k">
                <dt>
                  <img :src="v.icon" alt>
                </dt>
                <dd>{{v.title}}</dd>
              </dl>
            </div>
          </li>
        </ul>
      </div>
    </main>
  </div>
</template>
<script>
import BScroll from "better-scroll";
import { mapState, mapActions } from "vuex";
export default {
  components: {},
  data() {
    return {
      scrollList: [], //右边每一个li所有的自变量
      scrollY: 0 //实时滚动的top值
    };
  },
  computed: {
    ...mapState(["searchList"]),
    //实时滚动的下表
    selectIndex() {
      return this.scrollList.findIndex((item, index) => {
        this._initLeftScroll(index);
        return item >= this.scrollY;
      });
    }
  },
  mounted() {
    this.getSearch();
    //右边滚动
    this.scrollRight();
    //左边滚动
    this.scrollLeft();
  },
  methods: {
    ...mapActions(["getSearch"]),
    //右边滚动
    scrollRight() {
      this.rightScroll = new BScroll(".shop-right", {
        click: true,
        probeType: 3
      });
    },
    //左边滚动
    scrollLeft() {
      this.scrollLeft = new BScroll(".shop-left", { click: true });
    },
    //左右联动
    _initLeftScroll(index) {
      let el = this.$refs.left.children[index];
      this.scrollLeft.scrollTo(el, 300, 0, -480);
    },
    //点击左边右边联动
    toRight(index) {
      let lis = this.$refs.left.children;
      this.rightScroll.scrollTo(0, -this.scrollList[index]);
    }
  },
  watch: {
    //监听右边所有li距离顶部高度的数值
    searchList() {
      this.$nextTick(() => {
        //计算高度，默认为0
        let top = 0;
        //每一块距离顶部的距离的集合
        let topArr = [];
        topArr.push(top);
        let lis = [...this.$refs.ul.children];
        lis.map((item, index) => {
          top += item.clientHeight;
          topArr.push(top);
        });
        this.scrollList = topArr;
      });
      //监听实时滚动的scrollY的距离
      this.rightScroll.on("scroll", pos => {
        this.scrollY = Math.abs(pos.y);
      });
    }
  }
};
</script>
<style scoped lang="">
.wrap {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  font-size: 16px;
}

header {
  height: 44px;
  border-bottom: 1px solid #ccc;
  text-align: center;
  line-height: 44px;
}
main {
  flex: 1;
  width: 100%;
  overflow: hidden;
  display: flex;
}
.left {
  width: 90px;
  /* overflow-y: scroll; */
  text-align: center;
  line-height: 50px;
  display: flex;
  flex-direction: column;
  list-style: none;
}
.left li {
  width: 100%;
  height: 50px;
}
.on {
  color: red;
  border-left: 1px solid red;
  background: blanchedalmond;
}
.right {
  width: 285px;
  display: flex;
  flex-direction: column;
}
.right li {
  width: 100%;
  margin-bottom: 10px;
  font-size: 14px;
}
.right li .item_name {
  width: 100%;
  height: 20px;
  line-height: 20px;
  display: flex;
  justify-content: space-between;
}
.right li .right-dl {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}
.right li .right-dl dl {
  width: 23%;
  height: 120px;

  margin-left: 4px;
  margin-top: 3px;
}
dl dt {
  width: 100%;
  height: 90px;
}
dl dt img {
  width: 100%;
  height: 100%;
}
dl dd {
  width: 100%;
  height: 20px;
  margin-top: 10px;
  text-align: center;
  line-height: 20px;
}
</style>
